package com.c.ctgproject.ui.service

import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.service.ServiceCommonOrderEntity
import com.c.ctgproject.entity.service.ServiceOrderDetailEntityV2
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.ServiceDetailLayout
import com.c.ctgproject.wiget.ServiceDetailPictureLayout
import com.c.ctgproject.wiget.common.CommonInputDialog
import com.c.ctgproject.wiget.dialog.InputDialog
import kotlinx.android.synthetic.main.activity_service_order_detail.*

/**
 *作者:LiBW
 *创建日期: 2021/6/30
 *描述: 客服订单详情 页面
 */
class ServiceOrderDetailActivity : BaseActivity(), InputDialog.InPutDialogCallBack, CommonInputDialog.CommonInPutDialogCallBack {

    private val mViewModel by lazy { ViewModelProvider(this)[ServiceOrderDetailViewModel::class.java] }
    override fun getLayout() = R.layout.activity_service_order_detail
    private val data by lazy { intent.getSerializableExtra(MyConstants.DATA) as ServiceCommonOrderEntity.DataBean.ListBean }
    override fun event() {


        mViewModel.getOrderDetail(data.suborderId.handlerNull(), data.enterMode)
        mViewModel.error.observe(this, {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            loading.dismiss()
            MyLog.d("订单详情回调:$it")
            handlerHttpResult(it) {
                val entity = getGson().fromJson(it, ServiceOrderDetailEntityV2::class.java).data
                entity?.let {
                    mContainerLL.addView(ServiceDetailLayout(this, "订单号", data.orderCode.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "创建时间", data.createTime.handlerNull()))
                    val statue = when (data.status) {
                        "0" -> "已取消"
                        "1" -> "待处理"
                        "2" -> "待物流处理"
                        "3" -> "已完成"
                        "4" -> "待沟通"
                        "5" -> "回退订单"
                        "6" -> "待自送"
                        else -> ""
                    }
                    //只有回退订单展示底部按钮 取消和待沟通按钮
                    mBottomLL.visibility = if (data.status == "5") View.VISIBLE else View.GONE

                    mContainerLL.addView(ServiceDetailLayout(this, "状态", statue))
                    mContainerLL.addView(ServiceDetailLayout(this, "车牌号", data.ownerPlate.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "品牌", data.carBrand.handlerNull()))
                    val identification = "行驶证${if (data.drivingLicense == "1") "✅" else "❌"} 登记证${if (data.registration == "1") "✅" else "❌"} 身份证复印件${if (data.idCardCopy == "1") "✅" else "❌"}"
                    mContainerLL.addView(ServiceDetailLayout(this, "证件", identification))
                    mContainerLL.addView(ServiceDetailLayout(this, "车主姓名", data.carOwner.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "车主电话", data.ownerPhone.handlerNull()))
                    val doesGuaranteed = if (entity.doesGuaranteed == "1") "是" else "否"
                    mContainerLL.addView(ServiceDetailLayout(this, "是否保号", doesGuaranteed))
                    val isOperatingCar = if (data.isOperatingCar == "1") "是" else "否"
                    mContainerLL.addView(ServiceDetailLayout(this, "是否营运车辆", isOperatingCar))
                    val paymentMethod = when (entity.paymentMethod) {
                        "1" -> "支付宝"
                        "2" -> "微信收款码"
                        "3" -> "银行卡"
                        "4" -> "现金"
                        else -> ""
                    }
                    mContainerLL.addView(ServiceDetailLayout(this, "收款方式", paymentMethod))
                    mContainerLL.addView(ServiceDetailLayout(this, "收件人", entity.recipient.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "收件人电话", entity.recipientPhone.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "收货地址", entity.mailingAddress.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "客服备注", entity.remark.handlerNull()))
                    mContainerLL.addView(ServiceDetailLayout(this, "业务员姓名", data.salesman.handlerNull()))

                    data.carPicture?.let abc@{ images ->
                        if (images.isEmpty()) return@abc
                        mContainerLL.addView(ServiceDetailPictureLayout(this, "业务员收车图片", images))
                    }


                    //如果是拖车有第二部分数据展示

                    if (data.enterMode == "1" && data.status != "0"&& data.status != "1") {
                        val isDelay = if (data.isDelay == "1") "是" else "否"
                        mContainerLL.addView(ServiceDetailLayout(this, "是否延迟派单", isDelay))
                        mContainerLL.addView(ServiceDetailLayout(this, "预计拖车时间", entity.planTrailerTime.handlerNull()))
                        val statue2 = when (entity.status) {
                            "1" -> "待处理"
                            "2" -> "待托运"
                            "3" -> "托运中"
                            "4" -> "已完成"
                            "5" -> "已退回"
                            else -> ""
                        }
                        mContainerLL.addView(ServiceDetailLayout(this, "状态", statue2))
                        mContainerLL.addView(ServiceDetailLayout(this, "车辆所在位置", entity.carAddress.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "送往企业", entity.lgsCompanyName.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "拖车司机姓名", entity.trailerDriverName.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "拖车司机电话", entity.trailerDriverPhone.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "拖车司机车牌", entity.trailerPlate.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "实际接车时间", entity.realCltTime.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "送达时间", entity.finishTime.handlerNull()))
                        mContainerLL.addView(ServiceDetailLayout(this, "拖车司机备注", entity.trailerRemark.handlerNull()))
                        entity.picUrl?.let abc@{ images ->
                            if (images.isEmpty()) return@abc
                            mContainerLL.addView(ServiceDetailPictureLayout(this, "司机拍照", images))
                        }
                    }
                    val text = "入场${if (entity.hasEntered == "1") "✅" else "❌"}  " +
                            "已过磅${if (entity.hasPound == "1") "✅" else "❌"}  " +
                            "残值已发${if (entity.hasResidual == "1") "✅" else "❌"}\n" +
                            "已出回收证明${if (entity.hasRecycleProof == "1") "✅" else "❌"}  " +
                            "已出注销证明${if (entity.hasCancellationProof == "1") "✅" else "❌"} " +
                            "手续已发${if (entity.hasProcedure == "1") "✅" else "❌"}"

                    mBottomText.text = text
                }
            }
        })


        if (data.enterMode == "1") {
            mTitleBar.setTitle("订单详情(拖车)")
        } else if (data.enterMode == "2") {
            mTitleBar.setTitle("订单详情(自送)")
        }

        mViewModel.commonResult.observe(this, {
            MyLog.d("通用回调:$it")
            handlerHttpResult(it) {
                showToast("提交成功")
                finish()
            }
        })
        mViewModel.cancelResult.observe(this, {
            MyLog.d("取消回调:$it")
            handlerHttpResult(it) {
                showToast("取消订单成功")
                finish()
            }
        })

    }

    var inputDialog: CommonInputDialog? = null
    override fun click() {
        //仅回退订单 可以取消
        mOrderCancelBTN.setOnClickListener {
            inputDialog = CommonInputDialog(this, "取消订单", "请输入取消订单备注")
            inputDialog?.show(supportFragmentManager, "")
        }

        mOrderTalkBTN.setOnClickListener {
            mViewModel.serviceOrderWaitTalk(data.suborderId.handlerNull(), data.carId.handlerNull())
        }
    }

    //取消订单Dialog内填入信息回调
    override fun dialogResultText(msg: String) {
        inputDialog?.dismiss()
        mViewModel.serviceOrderCancel(data.suborderId.handlerNull(), data.carId.handlerNull(), msg)
    }


}